package edu.service.impl;

import edu.service.ScoreRatioService;
import edu.vo.ScoreRatio;
import edu.vo.ID.ScoreRatioId;
import edu.repository.ScoreRatioRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ScoreRatioServiceImpl implements ScoreRatioService {

    private final ScoreRatioRepository scoreRatioRepository;

    public ScoreRatioServiceImpl(ScoreRatioRepository scoreRatioRepository) {
        this.scoreRatioRepository = scoreRatioRepository;
    }

    @Override
    public ScoreRatio saveScoreRatio(ScoreRatio scoreRatio) {
        return scoreRatioRepository.save(scoreRatio);
    }

    @Override
    public Optional<ScoreRatio> getScoreRatioById(ScoreRatioId id) {
        return scoreRatioRepository.findById(id);
    }

    @Override
    public List<ScoreRatio> getAllScoreRatios() {
        return scoreRatioRepository.findAll();
    }

    @Override
    public void deleteScoreRatioById(ScoreRatioId id) {
        scoreRatioRepository.deleteById(id);
    }

    @Override
    public ScoreRatio updateScoreRatio(ScoreRatioId id, ScoreRatio scoreRatio) {
        return scoreRatioRepository.findById(id)
                .map(existingScoreRatio -> {
                    existingScoreRatio.setRatio(scoreRatio.getRatio());
                    return scoreRatioRepository.save(existingScoreRatio);
                })
                .orElse(null);
    }
}